import re

from django.views import View
# 导入返回响应
from SaaSDevelTool.data_return_response import create_response
# 导入错误信息装饰器
from SaaSDevelTool.data_return_response import error_message_detection
# 获取数据库连接
from SaaSDevelTool.data_mysql_data import db
# 判断参数类型
from SaaSDevelTool.data_operation import is_number

"""
获取当前企业的用户
"""


class SaaSDataTenementRegulatorUserQuery(View):
    @error_message_detection
    def get(self, request):
        # 获取当前企业编号
        tenement_id = request.GET.get("tenement_id")
        # 判断企业编号格式
        if not is_number(tenement_id):
            return create_response(code=400, msg="操作失败，企业编号类型错误。")
        # 拼接查询的sql语句
        query_base_info_contact_user_sql = """
            SELECT 
                ssumt.user_id as id,
                ssumt.user_mobile,
                CONCAT('名称:【',u.user_name,'】尾号:【',SUBSTRING(ssumt.user_mobile, -4),'】') AS modified_user_name 
            FROM 
                db_tenement_{}.base_info_contact_user u 
            JOIN  
            	db_saas_center.sum_saas_user_message_table ssumt
			ON 
				u.id = ssumt.user_id
			AND  
				ssumt.user_tenement_id = {}
            WHERE 
                u.manage_status = 1 
            ;""".format(tenement_id, tenement_id)
        # 获取查询的结果集
        query_base_info_contact_user_data = db.findAll(query_base_info_contact_user_sql)
        # 返回对应的响应
        return create_response(code=200, msg="", data=query_base_info_contact_user_data)
